var ArticleEditWindow = {
	articleId:0,
	returnBackTo:''
};
ArticleEditWindow.init = function(){
	windowManager.newWindow('article-edit-container', 'articleEdit');
}
ArticleEditWindow.open = function(){
	windowManager.windowShow('articleEdit', ArticleEditWindow);
}
ArticleEditWindow.close = function(){
	windowManager.windowHide('articleEdit');
	ArticleEditWindow._unBindEvents();
}

ArticleEditWindow._onOpened = function(){
	var art = {title:'', content:'', category:0};
	
	if ( 0 != ArticleEditWindow.articleId )
		art = Article.getInfo(ArticleEditWindow.articleId);
	
	$('#article-write-title').val(art.title);
	
	for ( var i=0; i<Article.category.categories.length; i++ )
		$('<option />', {value:Article.category.categories[i].id, html:Article.category.categories[i].name})
			.appendTo('#article-write-category');
	
	$('#article-write-category').val(art.category);
	
	if ( 0 == ArticleEditWindow.articleId || null != art.content ){
		$('#article-write-content').val(art.content);
	}else
		Article.getContent(ArticleEditWindow.articleId, function( content ){
			$('#article-write-content').val(content);
		});

	ArticleEditWindow._bindEvents();
	
	$('#article-write-save').button();
	$('#article-write-return').button();
	
	$('#article-write-content').htmlarea({css:'skin/smoothness/editor.css', toolbar:{}});
	$('#article-write-content-container .jHtmlArea .ToolBar').hide();
	$('#article-write-content-container .jHtmlArea div iframe').css('border', '0');
	
	$('#article-write-title').focus().trigger('mouseenter');
}
ArticleEditWindow._onReturnBackClicked = function(){
	ArticleEditWindow.close();
	if ( 'articleRead' == ArticleEditWindow.returnBackTo ){
		ArticleReadWindow.articleId = ArticleEditWindow.articleId;
		ArticleReadWindow.open();
	}
	else if ('articleMain' == ArticleEditWindow.returnBackTo){
		ArticleMainWindow.open();
	}
	else{
		alert('ArticleEditWindow.returnBackTo is not right, please check.');
	}
}
ArticleEditWindow._onSaveClicked = function(){
	var content = $('#article-write-content').val();
	var title = $('#article-write-title').val();
	
	if ( '' == title ){
		for(var i=0; i<3; i++)
			$('#article-write-title').effect('highlight', {}, 500, function(){});
		return;
	}
	
	if ( '' == content ){
		for(var i=0; i<3; i++)
			$('#article-write-content-container').effect('highlight', {}, 500, function(){});
		return;
	}
	
	Article.create(title, $('#article-write-category').val(), content, function( resultCode ){
		ArticleEditWindow._onReturnBackClicked();
	});
}
ArticleEditWindow._onTileMouseEnter = function(){
	setTimeout(function(){
		$('#article-write-title').trigger('mouseleave');
	}, 3000);
}

ArticleEditWindow._bindEvents = function(){
	$('#article-write-return').click(function(){
		ArticleEditWindow._onReturnBackClicked();
	});
	
	$('#article-write-save').click(function(){
		ArticleEditWindow._onSaveClicked();
	});
	
	$('#article-write-title').mouseenter(function(){
		ArticleEditWindow._onTileMouseEnter();
	});
}
ArticleEditWindow._unBindEvents = function(){
	$('#article-write-return').unbind('click');
	$('#article-write-save').unbind('click');
	$('#article-write-title').unbind('mouseenter');
}
